﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Drawing;

namespace Game1_Resist50
{
    class GraphicsHelper
    {
        // Singleton.
        static private GraphicsHelper _gHelper = null;
        protected GraphicsHelper() 
        {
            Debug.Assert(GraphicsHelper.BmpHeight > 0 && GraphicsHelper.BmpWidth > 0);
            _wholeImage = new Bitmap(GraphicsHelper.BmpWidth, GraphicsHelper.BmpHeight);
            Debug.Assert(_wholeImage != null);
            _gDrawer = Graphics.FromImage(_wholeImage);
            ClearBackground();
        }
        static public GraphicsHelper Instance()
        {
            if (_gHelper == null)
            {
                _gHelper = new GraphicsHelper();
            }

            Debug.Assert(_gHelper != null);
            return _gHelper;
        }


        //
        private Bitmap _wholeImage = null;
        public Bitmap WholeImage
        {
            get
            {
                Debug.Assert(_wholeImage != null);
                return _wholeImage;
            }
        }
        static private int _bmpWidth = 0;
        static public int BmpWidth
        {
            set
            {
                _bmpWidth = value;
            }
            get
            {
                return _bmpWidth;
            }
        }
        static private int _bmpHeight = 0;
        static public int BmpHeight
        {
            get
            {
                return _bmpHeight;
            }
            set
            {
                _bmpHeight = value;
            }
        }
        private Graphics _gDrawer = null;

        // API
        public void DrawObject(Bitmap bmpObject, Point cordinate)
        {
            DrawObject(bmpObject, cordinate.X, cordinate.Y);
        }
        public void DrawObject(Bitmap bmpObject, int x, int y)
        {
            Debug.Assert(bmpObject != null);
            if (x >= 0 && x < GraphicsHelper.BmpWidth && y >= 0 && y <= GraphicsHelper.BmpHeight)
            {
                _gDrawer.DrawImage(bmpObject, x, y, bmpObject.Width, bmpObject.Height);
            }
        }
        public void ClearBackground(Color color)
        {
            _gDrawer.Clear(color);
        }
        public void ClearBackground()
        {
            _gDrawer.Clear(Color.Black);
        }
        public void DrawAllGameObjects()
        {
            var objects = GameObjectsManager.Instance().ObjectsList;
            foreach (var obj in objects)
            {
                DrawObject(obj.ObjectImage(), obj.X, obj.Y);
            }
        }
    }
}
